﻿using AutoMapper;
using DocumentFormat.OpenXml.Vml;
using Domain.Factory_Modeling;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Dto.Factory_Modeling.OutPut;
using ReturnValue;

namespace MES.Server.API.Application.Command.Factory_Modeling
{
    public class GetWorkShopHander : IRequestHandler<GetWorkShopCommand, ApiPaging<GetWorkShopOutPut>>
    {
        private readonly IRepository<WorkShopModel> workShopRepo;
        private readonly IMapper mapper;
        public GetWorkShopHander(IRepository<WorkShopModel> workShopRepo, IMapper mapper)
        {
            this.workShopRepo = workShopRepo;
            this.mapper = mapper;   
        }
        /// <summary>
        /// 获取车间信息列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiPaging<GetWorkShopOutPut>> Handle(GetWorkShopCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiPaging<GetWorkShopOutPut>();
            var workShop = workShopRepo.GetAll().Where(x => x.IsDel == false);
            var zhi = mapper.Map<List<GetWorkShopOutPut>>(workShop);
            result.TotalPage = zhi.OrderByDescending(x => x.CreateTime).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Totalcount = zhi.Count;
            result.Code = StatusCode.Success;
            return Task.FromResult(result);
        }
    }
}
